<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>定时任务</title>
</head>
<style>
    *{font-size: 12px;}
    td img {width:80px; height:80px}
    table tr th {border-bottom: 1px solid #aaaaaa; background: #eeeeee; min-width: 85px;}
    table tr td {border-bottom: 1px solid #cccccc; min-width: 85px;}
    .odd{background: #96c6f742}
    .even{background: #fffb7a30}
</style>

<body th:with="nowDate=${#dates.format(new java.util.Date().getTime(), 'yyyy-MM-dd')}">
<h1>
    <a th:href="@{/addJob}" target="_self">新增</a>
    &nbsp;&nbsp;&nbsp;
</h1>
<table>
    <thead>
        <tr>
            <th th:text="序号">NO</th>
            <th th:text="任务ID">jobId</th>
            <th th:text="任务名">jobName</th>
            <th th:text="类路径">className</th>
            <th th:text="方法">method</th>
            <th th:text="参数">params</th>
            <th th:text="cron表达式">cron</th>
            <th th:text="状态">state</th>
            <th th:text="创建时间">created</th>
            <th th:text="创建人">createdBy</th>
            <th th:text="下次执行时间">nextExecTime</th>
            <th th:text="操作">jobLog</th>
        </tr>
    </thead>
    <tbody>
        <tr th:each="baseJob,iterStat: ${allJob}" th:class="${iterStat.odd}? 'odd':'even'">
            <td th:text="${iterStat.count}">count</td>
            <td th:text="${baseJob.jobId}">jobId</td>
            <td th:text="${baseJob.jobName}">jobName</td>
            <td th:text="${baseJob.className}">className</td>
            <td th:text="${baseJob.method}">method</td>
            <td th:text="${baseJob.params}">params</td>
            <td th:text="${baseJob.cron}">cron</td>
            <td>
                <span th:if="${baseJob.state} eq 1" th:text="有效">state</span>
                <span th:if="${baseJob.state} eq 0" th:text="无效">state</span>
            </td>
            <td th:text="${baseJob.created}">created</td>
            <td th:text="${baseJob.createdBy}">createdBy</td>
            <td th:text="${#dates.format(baseJob.nextExecTime, 'yyyy-MM-dd HH:mm:ss')}">nextExecTime</td>
            <td>
                <a href="javascript:void(0);" th:onclick="del([[${baseJob.jobId}]]);">删除</a>
                &nbsp;&nbsp;&nbsp;&nbsp;
                <a th:href="@{/editJob(jobId = ${baseJob.jobId})}" target="_self">编辑</a>
                &nbsp;&nbsp;&nbsp;&nbsp;
                <span th:id="execTask_+${baseJob.jobId}">
                    <span th:if="${baseJob.execStatus} == 2">执行中...</span>
                    <span th:if="${baseJob.execStatus == 1 or baseJob.execStatus==0}">
                        <a href="javascript:void(0);" th:onclick="execTask([[${baseJob.jobId}]]);">手动执行</a>
                    </span>
                </span>
                &nbsp;&nbsp;&nbsp;&nbsp;
                <a th:href="@{/getJobLogPage(jobId=${baseJob.jobId})}" target="_self">日志</a>
            </td>
        </tr>
    </tbody>
</table>
</body>
<script type="text/javascript" th:inline="javascript">
    /*<![CDATA[*/
    var xmlHttp;
    var delUrl= /*[[@{/delJob}]]*/ null;
    var execTaskUrl=/*[[@{/task/execTask}]]*/ null;
    var jobId;

    var execTask = function (jobId) {
        this.jobId = jobId;
        xmlHttp = new XMLHttpRequest();
        xmlHttp.onreadystatechange = function () {execTaskCallback();};
        xmlHttp.open("POST", execTaskUrl+"?jobId="+jobId, true);
        xmlHttp.send();
    }
    var execTaskCallback = function() {
        if (xmlHttp.readyState==0
            || xmlHttp.readyState==1
            || xmlHttp.readyState==2
            || xmlHttp.readyState==3) {
            var execTask = document.getElementById("execTask_"+jobId);
            execTask.innerHTML="执行中..."
        } else if(xmlHttp.readyState == 4) {
            var result = JSON.parse(xmlHttp.responseText);
            alert(result.message);
            window.location.reload()  //刷新页面
        }
    }

    var del = function (jobId) {
        if(window.confirm("确认删除？")) {
            xmlHttp = new XMLHttpRequest();
            xmlHttp.onreadystatechange = function(){delCallback();};
            xmlHttp.open("POST", delUrl+"?jobId="+jobId, true);
            xmlHttp.send();
            return true
        } else {
            return false;
        }

    }
    var delCallback = function() {
        if (xmlHttp.readyState==4) {
            if (xmlHttp.status == 200) {
                window.location.reload()  //刷新页面
            }
        }
    }
    /*]]>*/
</script>
</html>
